package com.ziyia.fragmentbasepracitce;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewsTitleFragment extends Fragment {
    private boolean isTwoPane = false;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.news_title_frag, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentActivity activity = getActivity();
        if (activity != null) {
            isTwoPane = activity.findViewById(R.id.newsContentLayout) != null;
        }
        List<News> newsList = new ArrayList<>();
        newsList.add(new News("年少有为，年少有为", "第一条数据"));
        newsList.add(new News("测试，测试，测试，测试", "第二条数据"));
        newsList.add(new News("嘿嘿，嘿嘿，哈哈哈", "第三条数据"));
        newsList.add(new News("测试就是测试，知道吗？", "第四条数据"));

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        RecyclerView recyclerView = activity.findViewById(R.id.newsTitleRecyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(new NewsAdapter(newsList));

    }


    class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
        private List<News> list;

        public NewsAdapter(List<News> list) {
            this.list = list;
        }

        @NonNull
        @NotNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getAdapterPosition();
                    News news = list.get(position);
                    if (isTwoPane) {
                        FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
                        NewsContentFragment fragment = (NewsContentFragment) supportFragmentManager.findFragmentById(R.id.newsContentFrag);
                        fragment.refresh(news.getTitle(), news.getContent());
                    } else {
                        NewsContentActivity.actionStart(parent.getContext(), news.getTitle(), news.getContent());
                    }
                }
            });
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull @NotNull NewsTitleFragment.NewsAdapter.ViewHolder holder, int position) {
            News news = list.get(position);
            holder.title.setText(news.getTitle());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            private TextView title;
            public ViewHolder(@NonNull @NotNull View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.newsTitle);
            }
        }
    }
}
